package com.fh.shop.api.member.controller;

import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConstant;
import com.fh.shop.api.member.biz.IMemberService;
import com.fh.shop.api.member.param.MemberAddParam;
import com.fh.shop.api.member.param.MemberUpdatePasswordParam;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/api/members")
@Api(tags = "会员controller")
public class MemberController {
    @Resource(name = "memberService")
    private IMemberService memberService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Value("${mail.success}")
    private String mailSuccess;
    @Value("${mail.error}")
    private String mailError;

    @PostMapping("/reg")
    public ServerResponse reg(@RequestBody MemberAddParam memberAddParam) {
        return memberService.reg(memberAddParam);
    }

    @GetMapping("/active/{uuid}")
    @ApiOperation("激活会员")
    @ApiImplicitParam(name = "uuid", value = "激活标识")
    public void activeMember(@PathVariable String uuid) throws IOException {
        boolean ok = memberService.activeMember(uuid);
        if (ok) {
            // 跨项目的页面跳转
            response.sendRedirect(mailSuccess);
        } else {
            response.sendRedirect(mailError);
        }
    }

    @PostMapping("/sendMail")
    @ApiOperation("发送邮件")
    @ApiImplicitParam(name = "mail", value = "邮箱")
    public ServerResponse sendMail(String mail) {
        return memberService.sendMail(mail);
    }

    @PostMapping("/login")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "memberName", value = "会员名"),
        @ApiImplicitParam(name = "password", value = "会员密码")
    })
    public ServerResponse login(String memberName, String password) {
        return memberService.login(memberName, password);
    }

    @PostMapping("/findMember")
    @NeedLogin
    public ServerResponse findMember() {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        String nickName = memberVo.getNickName();
        return ServerResponse.success(nickName);
    }

    @PostMapping("/logout")
    @NeedLogin
    public ServerResponse logout() {
        // 删除redis中对应的key
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long id = memberVo.getId();
        // 删除redis中的标志位
        RedisUtil.delete(KeyUtil.buildMemberKey(id));
        return ServerResponse.success();
    }

    @PostMapping("/updatePassword")
    @NeedLogin
    @ApiOperation("修改个人密码")
    @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header")
    public ServerResponse updatePassword(MemberUpdatePasswordParam memberUpdatePasswordParam) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long id = memberVo.getId();
        return memberService.updatePassword(memberUpdatePasswordParam, id);
    }
}
